package com.XinYun.Library.modules.Login.Dao.Impl;

import com.XinYun.Library.modules.Login.Dao.MemberDao;
import com.XinYun.Library.utils.SqlUtils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MemberDaoImpl implements MemberDao {
    private static Connection connection;

    /**
     * 根据学号查询用户密码
     * @param sid
     * @return
     * @throws Exception
     */
    @Override
    public String queryPwd(String sid) throws Exception {
        String password = null;
        Connection connection = DruidUtil.getConnection();
        String sql = "select password from register where sid = ?;";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,sid);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            password = rs.getString("password");
        }
        DruidUtil.close(rs,ps,connection);
        return password;
    }

    /**
     * 查看该学号是否注册成功
     * @param sid
     * @return
     * @throws Exception
     */
    @Override
    public int queryId(String sid) throws Exception {
        int id = 0;
        connection = DruidUtil.getConnection();
        String sql = "select id from register where sid = ?;";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,sid);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            id = rs.getInt("id");
        }
        DruidUtil.close(rs,ps,connection);
        return id;
    }

}
